/*
    \file    adxl355.c
    \brief   collect code for adxl355
*/
#include "main.h"
#include "adxl355.h"

extern RingBuffer *fifo_x;
extern RingBuffer *fifo_y;
extern RingBuffer *fifo_z;
extern RingBuffer *fifo;
/* configure the TIMER peripheral */
void timer1_config(void);
/* configure the TIMER1 interrupt */
static void timer1_nvic_config(void);

/**
    \brief      configure the TIMER1 interrupt
    \param[in]  none
    \param[out] none
    \retval     none
*/
static void timer1_nvic_config(void)
{
    nvic_priority_group_set(NVIC_PRIGROUP_PRE1_SUB3);
    nvic_irq_enable(TIMER1_IRQn, 3, 3);
}

/**
    \brief      configure the TIMER peripheral
    \param[in]  none
    \param[out] none
    \retval     none
*/
void timer1_config(void)
{
    /* ----------------------------------------------------------------------------
    TIMER1 Configuration:
    TIMER1CLK = SystemCoreClock/10800 = 10KHz, the period is 0.5s(5000/10000 = 0.5s).
    ---------------------------------------------------------------------------- */
    timer_parameter_struct timer_initpara;

    rcu_periph_clock_enable(RCU_TIMER1);

    timer_deinit(TIMER1);
    /* initialize TIMER init parameter struct */
    timer_struct_para_init(&timer_initpara);
    /* TIMER1 configuration */
    timer_initpara.prescaler = 10 - 1;
    timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
    timer_initpara.counterdirection = TIMER_COUNTER_UP;
    timer_initpara.period = 2700 - 1;
    timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
    timer_init(TIMER1, &timer_initpara);

    timer_interrupt_flag_clear(TIMER1, TIMER_INT_FLAG_UP);
    timer_interrupt_enable(TIMER1, TIMER_INT_UP);
    timer_enable(TIMER1);
}
/*!
    \brief      this function handles TIMER1 interrupt request.
    \param[in]  none
    \param[out] none
    \retval     none
*/
extern uint32_t pps_count;
rt_uint8_t sensor_data[96 * 3];
uint32_t raw_x[32];
uint32_t raw_y[32];
uint32_t raw_z[32];
void TIMER1_IRQHandler(void)
{
    uint8_t count = 0;
		rt_base_t level0;

    rt_interrupt_enter();
    if (SET == timer_interrupt_flag_get(TIMER1, TIMER_INT_FLAG_UP))
    {
        /* clear channel 0 interrupt bit */
        timer_interrupt_flag_clear(TIMER1, TIMER_INT_FLAG_UP);
				/* 关闭中断 */
				level0 = rt_hw_interrupt_disable();
        count = adxl355_get_raw_fifo_data(sensor_data, raw_x, raw_y, raw_z);
        //将数据写进Ringbuffer中
        if(RingBuffer_IsFull(fifo) == true)
        {
//						RingBuffer_Reset(fifo_x);
//						RingBuffer_Reset(fifo_y);
//						RingBuffer_Reset(fifo_z);
						RingBuffer_Reset(fifo);
        }
        else
        {
            if (count >0 && (RingBuffer_In(fifo, raw_x, count) > 0))
            {
                rt_kprintf("X%d\r\n", RingBuffer_Avail(fifo));
            }
            if(count >0 && (RingBuffer_In(fifo, raw_y, count) > 0))
            {
                rt_kprintf("Y%d\r\n", RingBuffer_Avail(fifo));
            }
						if(count >0 && (RingBuffer_In(fifo, raw_z, count) > 0))
						{
								rt_kprintf("Z%d\r\n", RingBuffer_Avail(fifo));
						}
        }
        pps_count += count;
				/* 恢复中断 */
				rt_hw_interrupt_enable(level0);
    }
    rt_interrupt_leave();
}

/*!
    \brief      main function
    \param[in]  none
    \param[out] none
    \retval     none
*/
int timer_test(void)
{
    timer1_config();
    timer1_nvic_config();
    rt_kprintf("timer1 begin\r\n");
		
		return 0;
}
// INIT_APP_EXPORT(timer_test);
MSH_CMD_EXPORT(timer_test, timer test); /*  INIT_BOARD_EXPORT */
